package com.minelx.animator.animators;

import java.util.List;

public class ToggleAnimator implements Animator { // FIXME 2020/4/15  wait for me!!!  addListener

	private final List<RepeatableAnimator> toggleItems;

	private Animator currentAnimator;

	private int toggleIndex;

	ToggleAnimator(List<RepeatableAnimator> toggleItems) {
		this.toggleItems = toggleItems;
		toggleIndex = 0;
	}

	@Override
	public void start() {
		if (isRunning())
			currentAnimator.stop();

		currentAnimator = toggleItems.get(toggleIndex);
		currentAnimator.start();
		nextToggle();
	}

	@Override
	public boolean isRunning() {
		return currentAnimator != null && currentAnimator.isRunning();
	}

	@Override
	public void stop() {
		currentAnimator.stop();
	}

	private void nextToggle() {
		toggleIndex++;
		toggleIndex %= toggleItems.size();
	}
}
